﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Unis.RuleBase.IFace;

namespace Unis.Framework.RuleBase.Attributes
{
    public sealed class AttributeConfigHelper
    {
        // Fields
        private static AttributeConfigHelper _Instance;
        private static object _Obj = new object();

        // Methods
        protected AttributeConfigHelper()
        {
        }

        public NextOwnAttribute[] GetNextOwnAttByType(object objTypeValue)
        {
            MemberInfo[] member = objTypeValue.GetType().GetMember(objTypeValue.ToString());
            if ((member == null) || (member.Length == 0))
            {
                return null;
            }
            List<NextOwnAttribute> list = new List<NextOwnAttribute>();
            foreach (object obj2 in member[0].GetCustomAttributes(false))
            {
                if (obj2.GetType().Equals(typeof(NextOwnAttribute)))
                {
                    list.Add(obj2 as NextOwnAttribute);
                }
            }
            return list.ToArray();
        }

        public ObjectDataMappingAttribute GetObjectDataMappingAttribute(object objTypeValue)
        {
            MemberInfo[] member = objTypeValue.GetType().GetMember(objTypeValue.ToString());
            if ((member == null) || (member.Length == 0))
            {
                return null;
            }
            ObjectDataMappingAttribute customAttribute = Attribute.GetCustomAttribute(member[0], typeof(ObjectDataMappingAttribute)) as ObjectDataMappingAttribute;
            if (customAttribute == null)
            {
                return null;
            }
            return customAttribute;
        }

        public ObjectRelationAttribute GetObjectRelationAttByType(object objTypeValue)
        {
            MemberInfo[] member = objTypeValue.GetType().GetMember(objTypeValue.ToString());
            if ((member == null) || (member.Length == 0))
            {
                return null;
            }
            ObjectRelationAttribute customAttribute = Attribute.GetCustomAttribute(member[0], typeof(ObjectRelationAttribute)) as ObjectRelationAttribute;
            if (customAttribute == null)
            {
                return null;
            }
            return customAttribute;
        }

        public ParentProviderAttribute GetParentProviderAttByType(object objTypeValue)
        {
            MemberInfo[] member = objTypeValue.GetType().GetMember(objTypeValue.ToString());
            if ((member == null) || (member.Length == 0))
            {
                return null;
            }
            ParentProviderAttribute customAttribute = Attribute.GetCustomAttribute(member[0], typeof(ParentProviderAttribute)) as ParentProviderAttribute;
            if (customAttribute == null)
            {
                return null;
            }
            return customAttribute;
        }

        public RuleSettingAttribute GetRuleSettingAtt(IBaseRule busObj)
        {
            RuleSettingAttribute customAttribute = Attribute.GetCustomAttribute(busObj.GetType(), typeof(RuleSettingAttribute)) as RuleSettingAttribute;
            if (customAttribute == null)
            {
                return null;
            }
            return customAttribute;
        }

        // Properties
        public static AttributeConfigHelper Instance
        {
            get
            {
                if (_Instance == null)
                {
                    lock (_Obj)
                    {
                        if (_Instance == null)
                        {
                            _Instance = new AttributeConfigHelper();
                        }
                    }
                }
                return _Instance;
            }
        }
    }
}
